<template>
<el-card class="box-card">
  <div slot="header" class="clearfix" style="padding: 0 20px;" >
    <span style="float:left;line-height:60px;">卡片名称</span>
    <el-button @click="goAdd()" style="float: right;margin-top:10px;" type="primary" round>添加文章</el-button>
    <el-button @click="exportExcel()" style="float: right;margin-top:10px;" type="primary" round>导出Excel</el-button>
  </div>
  <el-table
    :data="tableData"
    border
    style="width: 100%;line-height: 60px;text-align:center;">
    <el-table-column
      fixed
      prop="title"
      label="标题">
    </el-table-column>
    <el-table-column
      prop="desc"
      label="描述">
    </el-table-column>
    <el-table-column
      prop="author"
      label="作者">
    </el-table-column>
    <el-table-column
      prop="count"
      label="阅读次数">
    </el-table-column>
    <el-table-column
      prop="createAt"
      label="创建时间">
    </el-table-column>
    <el-table-column
      fixed="right"
      label="操作"
      width="200px"
      >
      <template slot-scope="scope">
        <el-button @click="delArtList( w)" type="danger" round size="small">删除</el-button>
        <el-button @click="eidtArtList(scope.row)" type="warning" round size="small">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    :page-size="count"
    background
    layout="prev, pager, next"
    @current-change="handleCurrentChange"
    :total="total">
  </el-pagination>
</el-card>
</template>

<script>
import { export_json_to_excel } from '@/Excel/Export2Excel'
import { getArtLists, delArt } from '@/api'
export default {
  data () {
    return {
      tableData: [],
      total: 0,
      page: 1,
      count: 10
    }
  },
  created () {
    this.GetArtLists()
  },
  methods: {
    // 点击删除
    delArtList (row) {
      this.id = row.id
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delArt(this.id).then(res => {
          if (res.data.code === '0') {
            this.$message({
              message: '删除成功',
              duration: 1000,
              onClose: () => {
                this.$router.go(0)
              }
            })
          }
        })
      }).catch(() => {
        this.$message({
          message: '已取消删除'
        })
      })
    },
    // 点击编辑
    eidtArtList (row) {
      this.$router.push(`/admin/artEdit/${row.id}`).catch(() => {})
    },
    // 获取文章列表数据
    GetArtLists () {
      getArtLists({ page: this.page, count: this.count }).then(res => {
        if (res.data.code === '0') {
          this.total = res.data.data.total
          const lists = res.data.data.lists
          lists.forEach(list => {
            list.createAt = new Date(list.createAt).toLocaleString().replace(/年|月/g, '-').replace(/日/g, ' ')
          })
          this.tableData = lists.splice((this.page - 1) * this.count, 10)
        }
      })
    },
    // 去添加页
    goAdd () {
      this.$router.push('/admin/artAdd').catch(() => {})
    },
    // 点击下面的那个分页按钮
    handleCurrentChange (val) {
      this.page = val
      this.GetArtLists()
    },
    // 导出的方法
    exportExcel () {
      require.ensure([], () => {
        // 注意这个Export2Excel路径
        const tHeader = ['标题', '描述', '作者', '阅读次数', '创建时间'] // 上面设置Excel的表格第一行的标题
        const filterVal = ['title', 'desc', 'author', 'count', 'createAt'] // 上面的index、nickName、name是tableData里对象的属性key值
        const list = this.tableData // 把要导出的数据tableData存到list
        const data = this.formatJson(filterVal, list)
        export_json_to_excel(tHeader, data, '列表excel')// 最后一个是表名字
      })
    },
    formatJson (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    }
  }

}
</script>

<style lang="less" scoped>
/deep/.clearfix{
  line-height: 0;
}
/deep/.el-card__header{
  border-bottom:0;
  padding: 0;
}
/deep/.el-card__body{
  padding:0;
}
.is-background{
  padding-top:45px;
  /deep/button{
    height:45px;
    width:45px;
  }
  /deep/ul>li{
    height:45px;
    width:45px;
    line-height:45px;
  }
}
</style>
